
#include "t1.h"
#include <stddef.h>
#include <string.h>

#if 0

t1_inf_t *t1_get_inf_field(t1_frame_t *frame) { return frame->inf; }

static uint8_t t1_calcLRC(uint8_t *apdu, uint8_t len) {
    uint8_t lrc = apdu[0];
    for (int i = 1; i < len; i++) {
        lrc ^= apdu[i];
    }
    return lrc;
}

struct {
    uint8_t seq;
} m_sam_t1_state = {1};

void t1_transmit(uint8_t *apdu, uint16_t len, uint16_t flags) {
    static uint8_t t1_tx_buff[512];
    t1_tx_buff[0] = 0x00;         // NAD
    t1_tx_buff[1] = 0x00 | flags; // PCB
    t1_tx_buff[2] = len;          // LEN
    memcpy(t1_tx_buff + 3, apdu, len);
    t1_tx_buff[len + 3] = t1_calcLRC(t1_tx_buff, len + 3);
    sam_transmit(t1_tx_buff, len + 4);

    if (flags == NULL) {
        m_sam_t1_state.seq++;
    }
}

SW1SW2_t t1_receive() {
    sam_receive();
    uint8_t *sam_rx_buffer = sam_getRxBuffer();
    m_sam_t1_state.seq = sam_rx_buffer[2] & 0x20 ? 1 : 0;
    uint16_t len = sam_rx_buffer[0];
    uint8_t SW1 = sam_rx_buffer[len - 2];
    uint8_t SW2 = sam_rx_buffer[len - 1];
    uint16_t SW1SW2 = (SW1 << 8) | SW2;

    return SW1SW2;
}
#endif 

